<?php

/**
 * 回访与重构
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';

class Admin_ReturnVisitController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_DayData();
        $this->_productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
                
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['source'] = trim($this->_request->get('source'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        
        $vars['dateType']  = trim($this->_request->get('dateType'))!='' ? trim($this->_request->get('dateType')) : 2;
        $vars['doType'] = 1;//trim($this->_request->get('doType'))!='' ? trim($this->_request->get('doType')) : 1;
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        
        if ($vars['dateType']==2){
            $vars['startTime'] = date('Y-m-d',strtotime("-7 day"));
            $vars['endTime']   = date('Y-m-d',strtotime("-1 day"));
        } elseif ($vars['dateType']==3){
            $vars['startTime'] = date('Y-m-d',strtotime("-30 day"));
            $vars['endTime']   = date('Y-m-d',strtotime("-1 day"));
        } elseif ($vars['dateType']==4){
            $vars['startTime'] = date('Y-m-d',strtotime("-3 months"));
            $vars['endTime']   = date('Y-m-d',strtotime("-1 day"));
        } elseif ($vars['dateType']==5){
            $vars['startTime'] = '';
            $vars['endTime']   = '';
        }
        $wheres[] = "`productCode` = '{$this->_productCode}'";
        $wheresFind[] = "a.`productCode` = '{$this->_productCode}' AND b.`productCode` = '{$this->_productCode}'";
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
            $wheresFind[] = "a.`date` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
            $wheresFind[] = "a.`date` <= '{$vars['endTime']}' ";
        }
        if ($vars['osName'] != '') {
            $wheres[] = "`osName` = '{$vars['osName']}' ";
        }
        if ($vars['source'] != '') {
            $wheres[] = "`sourceId` IN (SELECT  ms.`sourceId` FROM `mob_source` AS ms INNER JOIN `mob_member` AS mm ON  ms.memberCode=mm.code   WHERE   mm.name='{$vars['source']}' AND ms.productCode='{$this->_productCode}') ";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '{$vars['sourceId']}' ";
        }
        $orderBy = " `date` ASC ";
        $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
        $whereFind = count($wheresFind) > 0 ? ' WHERE ' . implode(' AND ', $wheresFind) . ' ' : '';
        $sql = "SELECT date,sum(installNum) as installNum,sum(bootClientNum) as bootClientNum,sum(installOkOrderNum) as installOkOrderNum,sum(okOrderNum) as okOrderNum 
                FROM {$this->_model->getTableName()} " . $where . 
                ' GROUP BY date ' .
                ' ORDER BY ' .$orderBy;
        $resTmp = $this->_model->getAdapter()->fetchAll($sql);
        $dayInstallNumArr = array();
        $returnVisitArr = array();
        $dayOrderNumArr = array();
        $returnBuyArr = array();
        $dateArr = array();
        foreach ($resTmp as $item) {
            $dateArr[] = $item['date'];
            $dayInstallNumArr[] = $item['installNum'];
            $returnVisitArr[] = $item['bootClientNum'] - $item['installNum'];
            $dayOrderNumArr[] = $item['okOrderNum'];
            $returnBuyArr[] = $item['okOrderNum'] - $item['installOkOrderNum'];
        }
        $highchart = array();
        $highchart['dayInstallNum'] = implode(',',$dayInstallNumArr);
        $highchart['returnVisit'] = implode(',',$returnVisitArr);
        $highchart['dayOrderNum'] = implode(',',$dayOrderNumArr);
        $highchart['returnBuy'] = implode(',',$returnBuyArr);
        $highchart['dateim'] = "'".implode("','",$dateArr)."'";
        $this->view->highchart = $highchart;
        $sqlFind = "SELECT a.productCode,a.sourceId,c.name,sum(a.installNum) as installNum 
                FROM {$this->_model->getTableName()} AS a 
                LEFT JOIN mob_source AS b  ON (a.sourceId = b.sourceId AND a.productCode = b.productCode)
                LEFT JOIN mob_member AS c ON (b.memberCode = c.code)
                " . $whereFind . 
                ' GROUP BY a.sourceId,c.name ' .
                ' ORDER BY installNum DESC';
        $sourceFindArr = $this->_model->getAdapter()->fetchAll($sqlFind);
        //列表内容
        $listArr = array();
        foreach ($resTmp as $item) {
            $listArr[$item['date']]['installNum'] = $item['installNum'];
            $listArr[$item['date']]['installOkOrderNum'] = $item['installOkOrderNum'];
            $listArr[$item['date']]['returnVisit'] = $item['bootClientNum'] - $item['installNum'];
            $listArr[$item['date']]['returnBuy'] = $item['okOrderNum'] - $item['installOkOrderNum'];
        }
        $endArr = array();
        foreach ($listArr as $key => $val) {
            $endArr['totalInstallNum'] = isset($endArr['totalInstallNum']) ? $endArr['totalInstallNum']+$val['installNum'] : $val['installNum'];
            $endArr['totalInstallOkOrderNum'] = isset($endArr['totalInstallOkOrderNum']) ? $endArr['totalInstallOkOrderNum']+$val['installOkOrderNum'] : $val['installOkOrderNum'];
            $endArr['totalReturnVisit'] = isset($endArr['totalReturnVisit']) ? $endArr['totalReturnVisit']+$val['returnVisit'] : $val['returnVisit'];
            $endArr['totalReturnBuy'] = isset($endArr['totalReturnBuy']) ? $endArr['totalReturnBuy']+$val['returnBuy'] : $val['returnBuy'];
        }
        $osNameRows = array(
                             array('iphone','iPhone'),
                             array('android','Android'),
                             array('symbian','Symbian'),
                             array('windowsphone','Windows Phone 7'),
                             array('androidpad','Android Pad'),
                             array('wap','Wap')
                             );
        $this->view->endArr = $endArr;
        $this->view->sourceFindArr = $sourceFindArr;
        $this->view->osNameRows = $osNameRowadaadaadads;
        $this->view->listArr = $listArr;
        $this->view->vars = $vars;
    }
    
   
}